package com.concurrency.example.deadLock.p2;

import java.awt.*;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 车辆调度中心
 */
public class Dispatcher {
    private final Set<Taxi> taxis;
    private final Set<Taxi> availableTaxis;

    public Dispatcher() {
        this.taxis = new HashSet<>();
        this.availableTaxis = new HashSet<>();
    }

    public synchronized void setTaxis(Taxi t) {
        taxis.add(t);
    }

    /**
     * 添加到可用集合
     * @param taxi
     */
    public synchronized void notifyAvailable(Taxi taxi) throws InterruptedException{
        TimeUnit.SECONDS.sleep(1);
        System.out.println("增加可用车辆");
        availableTaxis.add(taxi);
    }

    /**
     * 获得所有车辆的位置
     * @return
     */
    public synchronized String getImage() throws InterruptedException{
        TimeUnit.SECONDS.sleep(1);
        StringBuilder sb = new StringBuilder();
        for (Taxi taxi : taxis) {
            sb.append(taxi.getLocation());
        }
        return sb.toString();
    }
}
